package android_testsuite.mytest.network_test;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import android_testsuite.R;
import android_testsuite.mytest.util.Const;
import android_testsuite.mytest.util.Executor;

/**
 * @author Ren Hui
 * @since 1.0.1.058
 */
public class SocketActivity extends Activity implements View.OnClickListener {
    private LinearLayout tcpLayout;
    private LinearLayout udpLayout;
    private EditText mTcpThreadNum;
    private EditText mUdpThreadNum;
    private EditText mIp;
    private Button mStartBtn;
    private TextView mSocketResult;
    private Executor mExecutor;
    private CheckBox mBoxTCP;
    private CheckBox mBoxUDP;
    private EditText mUdpLength;
    private EditText mTcpLength;

    private int mThreadNum = 0;
    private int mLength = 0;
    private String mErr;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Const.ON_LOG_MSG:
                    mSocketResult.setText(mSocketResult.getText() + (String) msg.obj);
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // 回调函数，先super。 父类走一边
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.mStartBtn = (Button) findViewById(R.id.bt_startSocket);
        this.mSocketResult = (TextView) findViewById(R.id.socket_result);
        this.tcpLayout = (LinearLayout) findViewById(R.id.tcpLayout);
        this.udpLayout = (LinearLayout) findViewById(R.id.udpLayout);
        this.mTcpThreadNum = (EditText) findViewById(R.id.TcpThredNum);
        this.mUdpThreadNum = (EditText) findViewById(R.id.UdpThreadNum);
        this.mUdpLength = (EditText) findViewById(R.id.udp_length);
        this.mTcpLength = (EditText) findViewById(R.id.tcp_length);
        this.mIp = (EditText) findViewById(R.id.IP);
        this.mStartBtn.setOnClickListener(this);
        this.tcpLayout.setVisibility(View.GONE);
        this.udpLayout.setVisibility(View.GONE);
        this.mBoxTCP = (CheckBox) findViewById(R.id.TcpFun);
        this.mBoxUDP = (CheckBox) findViewById(R.id.UdpFun);
        this.mIp.setText(Const.HOST);

        this.mExecutor = new Executor(Const.MAX_POOL_SIZE);

        mBoxTCP.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton,
                                         boolean b) {
                if (!tcpLayout.isShown() && (b == true)) {
                    tcpLayout.setVisibility(View.VISIBLE);
                }

                if (tcpLayout.isShown() && (b == false)) {
                    tcpLayout.setVisibility(View.GONE);
                }
            }

        });

        mBoxUDP.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton,
                                         boolean b) {
                if (!udpLayout.isShown() && (b == true)) {
                    udpLayout.setVisibility(View.VISIBLE);
                }

                if (udpLayout.isShown() && (b == false)) {
                    udpLayout.setVisibility(View.GONE);
                }
            }
        });
    }


    @Override
    public void onClick(View view) {


        Runnable runnable = null;
        String ip = this.mIp.getText().toString();

        if (view.getId() == R.id.bt_startSocket) {
            if (tcpLayout.getVisibility() == View.VISIBLE) {
                String strNumber = this.mTcpThreadNum.getText().toString();
                String strLength = this.mTcpLength.getText().toString();
                boolean flag_ThreadNum = analyseNumberInput(strNumber, 0); //需要判断大小
                boolean flag_length = analyseNumberInput(strLength, 1);  //不需要判断大小
                if (flag_ThreadNum == true && flag_length == true) {
                    mThreadNum = Integer.valueOf(strNumber);
                    mLength = Integer.valueOf(strLength);
                    for (int i = 0; i < mThreadNum; i++) {
                        runnable = new TcpRunnable(this, ip, mLength);
                        this.mExecutor.start(runnable);
                    }

                } else {
                    Toast.makeText(this, mErr, Toast.LENGTH_SHORT).show();
                }
            }

            if (udpLayout.getVisibility() == View.VISIBLE) {
                String strNumber = this.mUdpThreadNum.getText().toString();
                String strLength = this.mUdpLength.getText().toString();

                boolean flag_ThreadNum = analyseNumberInput(strNumber, 0);//进程数需要判断大小
                boolean flag_length = analyseNumberInput(strLength, 1);  //包不判断大小
                if (flag_ThreadNum == true && flag_length == true) {
                    mThreadNum = Integer.valueOf(strNumber);
                    mLength = Integer.valueOf(strLength);
                    for (int i = 0; i < mThreadNum; i++) {
                        runnable = new UdpRunnable(this, ip, mLength);
                        this.mExecutor.start(runnable);
                    }

                } else {
                    Toast.makeText(this, mErr, Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mExecutor.close();
    }

    public void onLogging(String msg) {
        this.mHandler.obtainMessage(Const.ON_LOG_MSG, msg).sendToTarget();
    }

    //判断输入数值是否正确
    private boolean analyseNumberInput(String number, int flag) {
        if (TextUtils.isEmpty(number)) {
            mErr = getResources().getString(R.string.empty);
            return false;
        } else {
            try {
                int res = Integer.valueOf(number);
                if (0 != flag || res < Const.MAX_THREAD) {
                    return true;
                } else {
                    mErr = getResources().getString(R.string.bigNumber) + Integer.toString(Const.MAX_THREAD);
                    return false;
                }
            } catch (NumberFormatException e) {
                Log.e("SocketActivity", e.toString());
                mErr = getResources().getString(R.string.notNumber);
                return false;
            }
        }
    }
}